﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace Project_2_1
{
    public partial class frmItemTypeAdd : DevExpress.XtraEditors.XtraForm
    {
        public frmItemTypeAdd()
        {
            InitializeComponent();
        }


        private void frmItemTypeAdd_Load(object sender, EventArgs e)
        {          
            TreeNode node = new TreeNode();
            AddChildNode(node, 0);            

            SqlConnection Cnn = new SqlConnection(@"Server=.\SQLEXPRESS;database=QTCSDL3;Integrated security=true");
            Cnn.Open();
            string st = "select * from Loai where parent = 0";
            DataTable dt = new DataTable();
            SqlDataAdapter oAdap = new SqlDataAdapter(st, Cnn);
            oAdap.Fill(dt);
            
            cboGroup.DataSource = dt;            
            cboGroup.DisplayMember = "TenLoai";
            cboGroup.ValueMember = "MaLoai";
            cboGroup.Text = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!KiemTraTextBox())
            {
                MessageBox.Show("Chua nhap day du thong tin");
            }
            else
            {
                if (!KiemTraTonTai())
                {
                    return;
                }
                else
                {
                    SqlConnection Cnn = new SqlConnection(@"Server=.\SQLEXPRESS;database=QTCSDL3;Integrated security=true");
                    Cnn.Open();
                    string st = "select * from Loai";
                    SqlCommand oCmd = new SqlCommand("sp_Loai_Insert", Cnn);
                    oCmd.CommandType = CommandType.StoredProcedure;
                    DataTable dt = new DataTable();
                    SqlDataAdapter oAdap = new SqlDataAdapter(st, Cnn);
                    oAdap.Fill(dt);
                    TreeNode node = new TreeNode();

                    string maloai = "", tenloai = "";
                    maloai = GetID(dt).ToString();
                    tenloai = GetName(trvDevice);
                    int parent = GetParent(trvDevice);
                    MessageBox.Show("ID = " + GetID(dt).ToString()
                                    + "\nName = " + GetName(trvDevice)
                                    + "\nPatrent = " + GetParent(trvDevice).ToString());

                    oCmd.Parameters.Add("@MaLoai", SqlDbType.Char).Value = maloai;
                    oCmd.Parameters.Add("@TenLoai", SqlDbType.NVarChar).Value = tenloai;
                    oCmd.Parameters.Add("@Parent", SqlDbType.Int).Value = parent;
                    oCmd.ExecuteNonQuery();

                    trvDevice.Nodes.Clear();
                    AddChildNode(node, 0);
                }
            }
        }

        int GetID(DataTable dt)
        {
            if (dt == null)
            {
                return 0;
            }
            else
            {
                DataRow dr = dt.Rows[dt.Rows.Count - 1];
                return Convert.ToInt32(dr[0].ToString()) + 1;
            }
        }

        string GetName(TreeView trv)
        {
            string ck="";
            foreach (TreeNode item in trv.Nodes)
            {
                if (item.Text == cboGroup.Text)
                {
                    ck = txtName.Text;
                    break;
                }                
            }
            return ck;
        }

        int GetParent(TreeView trv)
        {
            int ck = 0;
            foreach (TreeNode item in trv.Nodes)
            {
                if (cboGroup.Text != item.Text)
                {
                    ck = 0;
                }
                else
                {
                    ck = item.Index + 1;
                    break;
                }
            }
            return ck;
        }

        private void AddChildNode(TreeNode parentNode, int parent)
        {
            string st = "Select * from Loai where parent =" + parent.ToString();
            SqlConnection Cnn = new SqlConnection(@"Server=.\SQLEXPRESS;database=QTCSDL3;Integrated security=true");
            Cnn.Open();
            SqlDataAdapter reader = new SqlDataAdapter(st,Cnn);
            DataTable dt = new DataTable();
            reader.Fill(dt);
            Cnn.Close();

            foreach (DataRow dr in dt.Rows)
            {
                TreeNode node = new TreeNode();
                node.Text = dr["TenLoai"].ToString();
                node.Tag = dr["MaLoai"].ToString();
                node.Expand();
                int childNode = Convert.ToInt32(dr["MaLoai"]);

                if (parentNode == null || parent == 0)
                {
                    AddChildNode(node, childNode);
                    trvDevice.Nodes.Add(node);
                }
                else
                {
                    AddChildNode(node, childNode);
                    parentNode.Nodes.Add(node);
                }
            }
        }

        bool KiemTraTextBox()
        {
            if (cboGroup.Text == "")
            {
                return false;                
            }
            else
            {
                foreach (TreeNode item in trvDevice.Nodes)
                {
                    if (cboGroup.Text == item.Text)
                        return true;
                }
                return true;
            }
        }

        bool KiemTraTonTai()
        {
            foreach (TreeNode parentNode in trvDevice.Nodes)
            {
                if (parentNode.Text == txtName.Text)
                {
                    MessageBox.Show("Tên loại hàng không được trùng với nhóm");
                    return false;
                }
                foreach (TreeNode childNode in parentNode.Nodes)
                {
                    if (childNode.Text == txtName.Text)
                    {
                        MessageBox.Show("Tên loại hàng đã có trong danh mục");
                        return false;
                    }                        
                }
            }
            return KiemTraTonTai();
        }

        private void trvDevice_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
        {
            
        }

        private void xoaToolStripMenuItem_Click(object sender, EventArgs e)
        {            
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (TreeNode item in trvDevice.Nodes)
            {
                MessageBox.Show(item.Text);
            }
        }       
    }
}